package com.mybatisflex.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.entity.Account;
import com.mybatisflex.mapper.AccountMapper;
import com.mybatisflex.util.EncryptUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * 账号相关
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    private final AccountMapper accountMapper;

    public AccountController(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @RequestMapping("/insert")
    public boolean insert(@RequestBody Account account) {
        account.setPassword(EncryptUtil.encrypt(account.getPassword()));
        return accountMapper.insert(account) > 0;
    }

    @RequestMapping("/remove")
    public int remove(Long id) {
        return accountMapper.deleteById(id);
    }

    @RequestMapping("/update")
    public int update(@RequestBody Account account) {
        return accountMapper.update(account);
    }

    @RequestMapping("/list")
    public List<Account> list() {
        System.out.println(StpUtil.isLogin());
        return accountMapper.selectAll();
    }

}
